﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.IO.Ports;

namespace TCA_User
{
    class Gridview_Timer_Group : SQL_APPL
    {
        private System.Windows.Forms.GroupBox Tab_Grp;
        private System.Windows.Forms.Label Date_to_Lbl;
        private System.Windows.Forms.Label Date_from_Lbl;
        private System.Windows.Forms.DateTimePicker dateTimePicker_To;
        private System.Windows.Forms.DateTimePicker dateTimePicker_From;
        private System.Windows.Forms.DataGridView dataGridView_View;
        private System.Windows.Forms.Button Review_BT;


        private string Database_Conn;
        private string SQL_Load_CMD;
        DataTable Taxi_List_dtb = new DataTable();
        DataSet Taxi_List_ds = new DataSet();
        SqlDataAdapter Taxi_List_da;

        public Gridview_Timer_Group(System.Windows.Forms.TabPage owner_tab, string tab_name, string connection_str, string sql_load_cmd)
        {
            Database_Conn = connection_str;
            SQL_Load_CMD = sql_load_cmd;
            Init_GrpBox(owner_tab, tab_name);
            Init_DatePicker(owner_tab, tab_name);
            Load_DataBase(Database_Conn);
        }

        private bool Init_GrpBox(System.Windows.Forms.TabPage owner_tab, string tab_name)
        {
            int height, width;
            height = owner_tab.Size.Height - 10;
            width = owner_tab.Size.Width - 320;

            Tab_Grp = new System.Windows.Forms.GroupBox();
            owner_tab.Controls.Add(Tab_Grp);
            this.Tab_Grp.AutoSize = true;
            this.Tab_Grp.SuspendLayout();
            this.Tab_Grp.Location = new System.Drawing.Point(6, 6);
            this.Tab_Grp.Name = tab_name;
            this.Tab_Grp.Size = new System.Drawing.Size(width, height);
            this.Tab_Grp.TabIndex = 0;
            this.Tab_Grp.TabStop = false;
            this.Tab_Grp.Text = tab_name;
            this.Tab_Grp.ResumeLayout(true);
            this.Tab_Grp.PerformLayout();
            this.Tab_Grp.AutoSize = false;
            this.Tab_Grp.Anchor = ((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top 
                                    | System.Windows.Forms.AnchorStyles.Bottom
                                    | System.Windows.Forms.AnchorStyles.Left));
            return true;
        }

        private bool Init_DatePicker(System.Windows.Forms.TabPage owner_tab, string tab_name)
        {
            dateTimePicker_To = new DateTimePicker();
            dateTimePicker_From = new DateTimePicker();
            Date_to_Lbl = new Label();
            Date_from_Lbl = new Label();
            dataGridView_View = new DataGridView();
            Review_BT = new Button();
            
            Tab_Grp.Controls.Add(dateTimePicker_To);
            Tab_Grp.Controls.Add(dateTimePicker_From);
            Tab_Grp.Controls.Add(Date_to_Lbl);
            Tab_Grp.Controls.Add(Date_from_Lbl);
            Tab_Grp.Controls.Add(dataGridView_View);
            Tab_Grp.Controls.Add(Review_BT);
            //owner_tab.Controls.Add(Review_BT);
                        
            Date_from_Lbl.Text = "From";
            Date_from_Lbl.Location = new System.Drawing.Point(10, 20);
            dateTimePicker_From.Location = new System.Drawing.Point(50, 15);

            Date_to_Lbl.Text = "To";
            Date_to_Lbl.Location = new System.Drawing.Point(255, 20);
            dateTimePicker_To.Location = new System.Drawing.Point(300, 15);

            dataGridView_View.Location = new System.Drawing.Point(10, 50);
            dataGridView_View.Size = new System.Drawing.Size(550, 290);
            dataGridView_View.Anchor = ((System.Windows.Forms.AnchorStyles)(
                                System.Windows.Forms.AnchorStyles.Bottom
                                | System.Windows.Forms.AnchorStyles.Left));

            Review_BT.Text = "Review";
            Review_BT.Location = new System.Drawing.Point(10, Tab_Grp.Size.Height - 30);
            Review_BT.Anchor = ((System.Windows.Forms.AnchorStyles)((
                                System.Windows.Forms.AnchorStyles.Bottom)
                                | System.Windows.Forms.AnchorStyles.Left));
            

            return true;
        }

        private bool Load_DataBase(string connection_str)
        {
            string sql_cmd = SQL_Load_CMD;
            //sql_cmd = sql_cmd + " WHERE [Bill_date] BETWEEN '" + dateTimePicker_From.Value.Date.ToString("dd MMM yy")
            //          + "' AND '" + dateTimePicker_To.Value.Date.ToString("dd MMM yy") + "'"; //'31/03/2010' AND '01/04/2010'

            Taxi_List_dtb.Clear();
            Taxi_List_dtb = Get_SQL_Data(connection_str, sql_cmd, ref Taxi_List_da, ref Taxi_List_ds);
            dataGridView_View.DataSource = Taxi_List_dtb;

            return true;
        }
    }
}
